package yu.liu.personal.app.myapplication.components.views;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.PopupWindow;

import java.util.ArrayList;
import java.util.List;

import yu.liu.personal.app.myapplication.components.PopupCollection;

/**
 * Created by yu.liu on 2016/9/28.
 */

public class FilterPanelPopupWindow extends PopupCollection {

    private final String infoType = "type";
    private final String infoStar = "star";
    private final String infoConsume = "consume";

    private List<CheckedTextView> typeList;
    private final int filterTypeCount = 6;
    private List<Boolean> typeCheckedFlags;
    private List<CheckedTextView> starList;
    private final int filterStarCount = 5;
    private List<Boolean> starCheckedFlags;
    private List<CheckedTextView> consumeList;
    private final int filterConsumeCount = 3;
    private List<Boolean> consumeCheckedFlags;
    private View btnConfirmFilter;
    private View areaOutboundFilter;

    private View.OnClickListener comboClickListener;

    public FilterPanelPopupWindow(Context context) {
        super(context);
        typeList = new ArrayList<>();
        starList = new ArrayList<>();
        consumeList = new ArrayList<>();
        comboClickListener = new comboButtonClickListener();
    }

    @Override
    public boolean showPopupWindow() {
        if(anchorView != null && windowView != null) {
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setContentView(windowView);
            showAsDropDown(anchorView);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void loadViews() {
        btnConfirmFilter = windowView.findViewById(resManager.getResourceID("id",
                "btnConfirmFilter"));
        areaOutboundFilter = windowView.findViewById(resManager.getResourceID("id",
                "areaOutboundFilter"));
        for(int i = 0; i < filterTypeCount; i++) {
            CheckedTextView ctv = (CheckedTextView) windowView.findViewById
                    (resManager.getResourceID("id", "comboTypeFilter_"+i));
            ctv.setTag("type,"+i);
            ctv.setOnClickListener(comboClickListener);
            typeList.add(ctv);
        }
        for(int i = 0; i < filterStarCount; i++) {
            CheckedTextView ctv = (CheckedTextView) windowView.findViewById
                    (resManager.getResourceID("id", "comboStarFilter_"+i));
            ctv.setTag("star,"+i);
            ctv.setOnClickListener(comboClickListener);
            starList.add(ctv);
        }
        for(int i = 0; i < filterConsumeCount; i++) {
            CheckedTextView ctv = (CheckedTextView) windowView.findViewById
                    (resManager.getResourceID("id", "comboConsumeFilter_"+i));
            ctv.setTag("consume,"+i);
            ctv.setOnClickListener(comboClickListener);
            consumeList.add(ctv);
        }
    }

    @Override
    public void loadMembers() {
        if(typeCheckedFlags == null) {
            typeCheckedFlags = new ArrayList<>();
            for(int i = 0; i < filterTypeCount; i++) {
                typeCheckedFlags.add(true);
                typeList.get(i).setChecked(true);
            }
        } else {
            for(int i = 0; i < filterTypeCount; i++) {
                typeList.get(i).setChecked(typeCheckedFlags.get(i));
            }
        }
        if(starCheckedFlags == null) {
            starCheckedFlags = new ArrayList<>();
            for(int i = 0; i < filterStarCount; i++) {
                starCheckedFlags.add(true);
                starList.get(i).setChecked(true);
            }
        } else {
            for(int i = 0; i < filterStarCount; i++) {
                starList.get(i).setChecked(starCheckedFlags.get(i));
            }
        }
        if(consumeCheckedFlags == null) {
            consumeCheckedFlags = new ArrayList<>();
            for(int i = 0; i < filterConsumeCount; i++) {
                consumeCheckedFlags.add(true);
                consumeList.get(i).setChecked(true);
            }
        } else {
            for(int i = 0; i < filterConsumeCount; i++) {
                consumeList.get(i).setChecked(consumeCheckedFlags.get(i));
            }
        }
    }

    @Override
    public void postSetup() {
        btnConfirmFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onConfirmClickListener != null) {
                    onConfirmClickListener.onConfirm();
                }
                dismiss();
            }
        });
        areaOutboundFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void updateViews() {

    }

    public void setTypeCheckedFlags(List<Boolean> typeCheckedFlags) {
        this.typeCheckedFlags = typeCheckedFlags;
    }

    public List<Boolean> getTypeCheckedFlags() {
        return typeCheckedFlags;
    }

    public void setStarCheckedFlags(List<Boolean> starCheckedFlags) {
        this.starCheckedFlags = starCheckedFlags;
    }

    public List<Boolean> getStarCheckedFlags() {
        return starCheckedFlags;
    }

    public void setConsumeCheckedFlags(List<Boolean> consumeCheckedFlags) {
        this.consumeCheckedFlags = consumeCheckedFlags;
    }

    public List<Boolean> getConsumeCheckedFlags() {
        return consumeCheckedFlags;
    }

    private class comboButtonClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            try {
                String info = (String) v.getTag();
                String[] detail = info.split(",");
                int index;
                switch(detail[0]) {
                    case infoType:
                        index = Integer.parseInt(detail[1]);
                        typeCheckedFlags.set(index, !typeCheckedFlags.get(index));
                        typeList.get(index).setChecked(typeCheckedFlags.get(index));
                        break;
                    case infoStar:
                        index = Integer.parseInt(detail[1]);
                        starCheckedFlags.set(index, !starCheckedFlags.get(index));
                        starList.get(index).setChecked(starCheckedFlags.get(index));
                        break;
                    case infoConsume:
                        index = Integer.parseInt(detail[1]);
                        consumeCheckedFlags.set(index, !consumeCheckedFlags.get(index));
                        consumeList.get(index).setChecked(consumeCheckedFlags.get(index));
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
